package com.ruoyi.web.controller.client;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.client.MesQualityItems;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IMesQualityItemsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 质量数据采集项定义Controller
 *
 * @author ruoyi
 * @date 2023-02-23
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mes/items" )
public class MesQualityItemsController extends BaseController {

    private final IMesQualityItemsService iMesQualityItemsService;

    /**
     * 查询质量数据采集项定义列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MesQualityItems mesQualityItems)
    {
        startPage();
        LambdaQueryWrapper<MesQualityItems> lqw = new LambdaQueryWrapper<MesQualityItems>();
        if (StringUtils.isNotBlank(mesQualityItems.getCollectItemName())){
            lqw.like(MesQualityItems::getCollectItemName ,mesQualityItems.getCollectItemName());
        }
        if (StringUtils.isNotBlank(mesQualityItems.getEquipmentPlc())){
            lqw.eq(MesQualityItems::getEquipmentPlc ,mesQualityItems.getEquipmentPlc());
        }
        if (mesQualityItems.getCollectDataType() != null){
            lqw.eq(MesQualityItems::getCollectDataType ,mesQualityItems.getCollectDataType());
        }
        List<MesQualityItems> list = iMesQualityItemsService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出质量数据采集项定义列表
     */
    @Log(title = "质量数据采集项定义" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(MesQualityItems mesQualityItems) {
        LambdaQueryWrapper<MesQualityItems> lqw = new LambdaQueryWrapper<MesQualityItems>(mesQualityItems);
        List<MesQualityItems> list = iMesQualityItemsService.list(lqw);
        ExcelUtil<MesQualityItems> util = new ExcelUtil<MesQualityItems>(MesQualityItems. class);
        return util.exportExcel(list, "items" );
    }

    /**
     * 获取质量数据采集项定义详细信息
     */
    @GetMapping(value = "/{collectItemId}" )
    public AjaxResult getInfo(@PathVariable("collectItemId" ) Long collectItemId) {
        return AjaxResult.success(iMesQualityItemsService.getById(collectItemId));
    }

    /**
     * 新增质量数据采集项定义
     */
    @Log(title = "质量数据采集项定义" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesQualityItems mesQualityItems) {
        mesQualityItems.setCreateBy(SecurityUtils.getUsername());
        mesQualityItems.setCreateTime(new Date());
        return toAjax(iMesQualityItemsService.save(mesQualityItems) ? 1 : 0);
    }

    /**
     * 修改质量数据采集项定义
     */
    @Log(title = "质量数据采集项定义" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesQualityItems mesQualityItems) {
        mesQualityItems.setUpdateBy(SecurityUtils.getUsername());
        mesQualityItems.setUpdateTime(new Date());
        return toAjax(iMesQualityItemsService.updateById(mesQualityItems) ? 1 : 0);
    }

    /**
     * 删除质量数据采集项定义
     */
    @Log(title = "质量数据采集项定义" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{collectItemIds}" )
    public AjaxResult remove(@PathVariable Long[] collectItemIds) {
        return toAjax(iMesQualityItemsService.removeByIds(Arrays.asList(collectItemIds)) ? 1 : 0);
    }
}
